package cn.flkj.lucia2coudb.config;

import lombok.extern.slf4j.Slf4j;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : 州长在手 2020/10/21 下午2:21
 */
@Configuration
@Slf4j
public class CouchDBConfig {
    final
    CouchDBInfo couchDBInfo;
    public CouchDBConfig(CouchDBInfo couchDBInfo) {
        this.couchDBInfo = couchDBInfo;
    }
    @Bean(name = "CouchDbConnector")
    public CouchDbConnector couchDbConnector() throws Exception {
        HttpClient httpClient = new StdHttpClient.Builder().url(couchDBInfo.getHost() + ":"+ couchDBInfo.getPort())
                .username(couchDBInfo.getUsername()).connectionTimeout(10000).socketTimeout(1000000)
                .password(couchDBInfo.getPassword()).build();
        CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);
        CouchDbConnector couchDbConnector = new StdCouchDbConnector(couchDBInfo.getDatabase(), couchDbInstance);
        couchDbConnector.createDatabaseIfNotExists();
        log.info("CouchDbConnector注入成功");
        return couchDbConnector;
    }
}
